﻿using System.Linq;
using System.Web.Mvc;
using Storytelling.Entidades.VTR;
using Storytelling.Modelos.VTR;
using Storytelling.Negocio;
using Storytelling.Utils;

namespace Storytelling.Controllers
{
    public class ClaController : BaseCrudController<Cla, ClaFormModel>
    {
        protected override IServico<Cla, ClaFormModel> Servico
        {
            get { return Servicos.Cla; }
        }

        protected override void EscolherCarregamento(Carga<Cla> carga, TipoCarregamento tipo)
        {
            carga.Carregar(ent => ent.Disciplinas).Carregar(ent => ent.Disciplina);
        }

        [AutorizarEditor]
        public override ActionResult Form(long? id)
        {
            return base.Form(id);
        }

        [AutorizarEditor]
        public override ActionResult Form(ClaFormModel model)
        {
            return base.Form(model);
        }

        [AutorizarEditor]
        public override ActionResult Excluir(long? id)
        {
            return base.Excluir(id);
        }

        [Authorize]
        public JsonResult CarregarHeranca(long? id)
        {
            int heranca = -1;

            if (id.HasValue)
            {
                heranca = (int) Servico.Consultar(id.Value).Heranca.Value;
            }

            return Json(heranca);
        }

        [Authorize]
        public JsonResult DisciplinaFamiliar(long? id, long? idDisciplina)
        {
            bool isFamiliar = false;

            if (id.HasValue)
            {
                Carga<Cla> carga = new Carga<Cla>();
                carga.Carregar(ent => ent.Disciplinas);
                isFamiliar = Servico.Consultar(id.Value, carga).Disciplinas.Any(ent => ent.DisciplinaId == idDisciplina);
            }

            return Json(isFamiliar);
        }
    }
}
